﻿/*
 *
 * Database.cs
 * For database.php in CI
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Controller, Model, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace cici
{
    class Database
    {

        private string path_application;
        private string database_file;
        private StreamWriter streamWritter;
        private StreamReader streamReader;

        public Database()
        {
            this.path_application = Environment.CurrentDirectory + @"\application\config";
            this.database_file = this.path_application + @"\database.php";
        }

        public string findConfigurationDatabase(string configFind)
        {
            string result=null;


            if (Directory.Exists(this.path_application))
            {
                if (File.Exists(this.database_file))
                {
                    this.streamReader = new StreamReader(this.database_file);

                    string temp_string = this.streamReader.ReadToEnd();

                    string active_group = "default";

                    Match temp_x = Regex.Match
(temp_string, "\\$db\\[?(\"|')" + active_group + "?(\"|')\\]\\[?(\"|')" + configFind + "?(\"|')\\]\\s=\\s.*");
                    string[] temp_abis = temp_x.Value.Split('=');

                    result = temp_abis[1].Replace("'", "").Replace(";", "");

                }
            }

            return result;
        }

        public Boolean createDatabase(string databaseConfig)
        {
            Boolean status = false;

            string[] temps = databaseConfig.Split(';');

            Dictionary<string, string> temp_dic = new Dictionary<string, string>();

            foreach (string temp in temps)
            {
                string[] temp_abis = temp.Split('=');
                if (temp_abis.Length > 1)
                {
                    temp_dic.Add(temp_abis[0], temp_abis[1]);
                }
            }

            if (Directory.Exists(this.path_application))
            {
                if (File.Exists(this.database_file))
                {

                    this.streamReader = new StreamReader(this.database_file);

                    string temp_string = this.streamReader.ReadToEnd();

                    string active_group = "default";

                    string output_regex = temp_string;

                    foreach (KeyValuePair<string, string> pair in temp_dic)
                    {
                        output_regex = Regex.Replace(output_regex, 
                            "\\$db\\[?(\"|')" + active_group + "?(\"|')\\]\\[?(\"|')" + pair.Key + "?(\"|')\\]\\s=\\s.*",
                            "$db['" + active_group + "']['" + pair.Key + "'] = '" + pair.Value + "';");
                    }

                    output_regex += "\n/* Generated by CICI */";

                    this.streamReader.Close();

                    this.streamWritter = new StreamWriter(this.database_file);
                    this.streamWritter.Write(output_regex);
                    this.streamWritter.Flush();
                    this.streamWritter.Close();

                    status = true;

                }
            }

            return status;

        }
    }
}
